<template>
  <div class="box">
    <HomeFirstClassification v-for="(item,index) in  firstList" :finalList="item" :key="index">

    </HomeFirstClassification>
  </div>
</template>

<script>
  import HomeFirstClassification from "../../../real-components/HomeFirstClassification";
  import {mapGetters} from "vuex";
  import {getPageTree, initDomTree} from "../../../utils/getPageTree";
  import axios from "axios";
  export default {
    props: {
      list: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        msg: '这是首页',
        page: "home",
        componentName: "HomeFirstClassification",

        recomendData: [],
        countLength: 0,
        firstList: null,
        currentFirstList: [{
          firstCode: '01',
          imageList: {
            activePicture: "",
            enable: 1,
            image: '',
          },
          prod: [{
            publicityPicture: '',
            relGroup: '01',
          }],
          title: {
            describe: "001",
            title: "01",
          }
        }],
        finalList: [],
      }
    },
    computed: {
      ...mapGetters([
        "getDomTree"
      ]),
      pageTree() {
        return getPageTree(this, this.page);
      }
    },
    components: {
      HomeFirstClassification,
    },
    methods: {
      querfinalList() {
        let url = window.api.goods_host + window.api.home_commend;
        let options = {
          method: "get",
          url,
          params: {
            enterpriseCode: api.enterpriseCode,
            storeCode: this.storeCodedata,
            usePlatform: "wap"
          },
        }
        axios(options).then(data => {
          var res = data.data.data[0]
          this.$store.commit('cjy/QUERY_CLASSIFY_BANNER', data.data)
          var firstList = this.rebuildData(res)
          if (firstList.length > 0) {
            this.firstList = this.rebuildData(res)
          } else {
            this.firstList = this.currentFirstList
          }
        })
      },
      rebuildData(data) {
        let list = []
        for (let key in data) {
          let value = data[key]
          let item = {
            firstCode: key
          }
          //查找标题
          let title = value.find((item) => {
              return item.relPosition == 'TITLE'
            }) || null
          //添加图片到新组合的对象
          item.title = {
            describe: title ? title.relSubtitle : '',
            title: title ? title.relTitle : '',
          }
          // 查找图片
          let image = value.find((item) => {
              return item.relPosition == 'IMAGE'
            }) || null
          item.imageList = {
            image: image ? image.publicityPicture : 'static/image/nodata',
            activePicture: image ? image.activePicture : '',
            enable: image ? image.promotionType : 1
          }
          let goods = value.filter(
            (item) => {
              return item.relPosition == 'GOODS'
            })
          let prod = goods.map((item) => {
              return item
              /*return {
               goodsNo: item.goodsCode,
               image: item.publicityPicture
               }*/
            }) || []
          item.prod = prod
          list.push(item)
        }
        /*console.log(list,333333)*/
        return list
      },

      /*queryRecomend() {
       let queryClassifyPic = api.local_host + "/resourceLevel/queryResourceLevel"
       axios({
       url: queryClassifyPic,
       method: "post",
       data: {
       "enterpriseId": api.enterpriseId,
       "pc": false
       }
       }).then((res) => {
       let source = res.data.data[0]
       if(source) {
       for(let key in source) {
       if(source[key].categoryName === '虚拟商品') {} else {
       this.countLength += 1
       let titleConfig = source[key].title
       let bannerConfig = source[key].imageList[0]
       let goodsConfig = source[key].prod
       if(source[key].title) {
       titleConfig.classificationId = source[key].classificationId
       }
       this.renderRecomendCate(titleConfig, bannerConfig, goodsConfig, key)
       }
       }
       }
       })
       },

       renderRecomendCate(titleConfig, bannerConfig, configData, index, classificationId) {
       let ids = ''
       configData && configData.map((item) => {
       if(item.goodsNo) {
       ids += item.goodsNo + ','
       }
       })
       let self = this
       if(ids) {
       axios.get(api.mall_goods_host + '/goodsRestApi/queryGoodsList?enterpriseid=' + api.enterpriseId + "&goodscode=" + ids).then((res) => {
       let dataSource = res.data.data
       dataSource && dataSource.map((item, i) => {
       let val = configData[i]
       var goodsNo = val.goodsNo
       var item = dataSource.find((ele) => {
       return parseInt(ele.goodscode) == parseInt(goodsNo)
       }) || {}

       //              console.log("index",i,"vae",goodsNo , item.goodscode)
       if(val) {
       val.goodsname = item.goodsname
       val.goodsspec = item.goodsspec
       val.goodsunit = item.goodsunit
       val.wsprice = item.wsprice
       val.normalprice = item.normalprice
       }
       })
       self.recomendData.push({
       recomendTitle: titleConfig,
       bannerImg: bannerConfig,
       goodsList: configData,
       index
       })
       //            console.log("self.recomendData", self.recomendData)
       if(self.countLength == self.recomendData.length) {
       self.sortList()
       }
       })
       } else {
       self.recomendData.push({
       recomendTitle: titleConfig,
       bannerImg: bannerConfig,
       goodsList: [],
       index
       })
       if(self.countLength == self.recomendData.length) {
       self.sortList()
       }
       }
       },

       sortList() {
       let list = this.recomendData
       list.sort(this.compare('index'))
       this.finalList = list
       //        sessionStorage.setItem( 'finalList', JSON.stringify(list))
       },

       compare(property) {
       return function(a, b) {
       var value1 = a[property];
       var value2 = b[property];
       return value1 - value2;
       }
       },*/

    },
    created() {
      var list = sessionStorage.getItem('finalList')
      if (list) {
        this.finalList = JSON.parse(list)
      } else {
        this.finalList = []
      }

    },
    mounted() {
      /*this.finalList.length == 0 && this.queryRecomend();*/
      this.querfinalList()
      PSEvent.listen("finalListUpdate", () => {
        this.querfinalList()
      });
    },
    updated() {
      this.$emit('refresh')
    }
  }
</script>

<style>

</style>
